//
//  RTStatusPickerView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/8.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit
import SDWebImage

private let itemMargin: CGFloat = 5
private let itemWH = CGFloat(Int(kScreenWidth - 2*kMargin - 2 * itemMargin)/3)

class RTStatusPickerView: UICollectionView {
    
    
    private lazy var identifier: String = "picCell"
    
    //配图数据
    var picUrls: [RTStatusPictureInfo]? {
    
        didSet {
        
            numLabel.text = "\(picUrls?.count ?? 0)"
    
            
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(sizeOfCount(picUrls?.count ?? 0))
            }
            
            self.reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // 加载内部组件
    private func setupUI() {
        
        backgroundColor = UIColor.orangeColor()
        
        // 注册cell
        registerClass(RTStatusPickerViewCell.self, forCellWithReuseIdentifier: identifier)
        
        self.dataSource = self
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        addSubview(numLabel)
        
        numLabel.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
    }
    
    
    
    // picture的大小
    private func sizeOfCount(count: Int)->CGSize {
        
        
        if count == 1 {
        
           if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picUrls!.first?.thumbnail_pic) {
            
            
                var size = image.size
            
            if size.width < 120 {
            
                size.height = 120 / size.width * size.height
                size.width = 120
            }
            
            let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
            
            layout.itemSize = size
            
            return size
            
            }
        
        }
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
    
        let col = count == 4 ? 2 :(count > 3 ? 3 :count)
        
        let row = (count-1)/3 + 1
        
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    }
    
    
    // 懒加载显示的label
    private lazy var numLabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
}



extension RTStatusPickerView: UICollectionViewDataSource {


    // 返回组数
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! RTStatusPickerViewCell
        
        cell.pictureInfo = picUrls![indexPath.item]
        
        return cell
    }
}



class RTStatusPickerViewCell: UICollectionViewCell {
    
    var pictureInfo: RTStatusPictureInfo? {
    
        didSet {
            
            guard let picStr = pictureInfo?.thumbnail_pic else {
                
                return
            }
            
            imageView .sd_setImageWithURL(NSURL(string: picStr), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 加载内部组件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    private func setupUI() {
    
        
        addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self)
        }
    }
    
    
    // 懒加载
    private lazy var imageView: UIImageView = {
        
        let imageView = UIImageView()
        
        imageView.contentMode = .ScaleAspectFill
        
        imageView.clipsToBounds = true
        
        return imageView
    }()
}
